﻿using System;
using System.Collections.Generic;
using System.Web;

namespace Redis
{
    public class NCacheService : ICacheService
    {
        #region Members
        private static NCacheService _instance;
        private readonly System.Web.Caching.Cache _cache;
        private static object _syncLock = new object();
        #endregion

        #region Methods
        private NCacheService()
        {
            _cache = HttpRuntime.Cache;
        }

        public static NCacheService Instance()
        {
            if (_instance == null)
            {
                lock (_syncLock)
                {
                    if (_instance == null)
                    {
                        _instance = new NCacheService();
                    }
                }
            }

            return _instance;
        }


        public bool Contains(string key)
        {
            return (_cache[key] != null);
        }

        public void Add(string key, object value)
        {
            _cache.Insert(key, value);
        }

        public void Add(string key, object value, DateTime expiry)
        {
            _cache.Insert(key, value, null, DateTime.Now.AddMinutes(1440), System.Web.Caching.Cache.NoSlidingExpiration);
        }

        public int Increment(string key, int defaultValue, int delta, int expireHours)
        {
            throw new NotImplementedException();
        }

        public ulong Increment(string key, ulong defaultValue, ulong delta, int expireHours)
        {
            throw new NotImplementedException();
        }

        public object Get(string key)
        {
            return _cache.Get(key);
        }

        public T Get<T>(string key)
        {
            return (T)_cache.Get(key);
        }

        public void Remove(string key)
        {
            _cache.Remove(key);
        }

        public void MultilDelete(List<string> lkey)
        {
            foreach(var key in lkey)
            {
                _cache.Remove(key);
            }
        }

        public IDictionary<string, object> Get(IEnumerable<string> keys)
        {
            var dictItems = new Dictionary<string, object>();
            foreach (var key in keys)
            {
                dictItems.Add(key, Get(key));
            }

            return dictItems;
        }

        public IDictionary<string, T> Get<T>(IEnumerable<string> keys)
        {
            throw new NotImplementedException();
        }

        public void Add(string key, object value, int expireHours)
        {
            _cache.Insert(key, value, null, DateTime.Now.AddHours(expireHours), System.Web.Caching.Cache.NoSlidingExpiration);
        }

        public void FlushAll()
        {
            var dictItems = _cache.GetEnumerator();
            Remove(dictItems.Key.ToString());
            while (dictItems.MoveNext())
            {
                Remove(dictItems.Key.ToString());
            }
        }

        #endregion
    }
}
